package views

import (
	"blog/common"
	"blog/context"
	"blog/service"
	"errors"
	"log"
	"strconv"
)

func (*HTMLApi) Category(context *context.MsContext) {
	categoryTemplate := common.Template.Category
	cIdStr := context.GetPathVariable("id") //分类id
	cId, err := strconv.Atoi(cIdStr)
	if err != nil {
		categoryTemplate.WriteError(context.W, errors.New("不识别此路径"))
		return
	}
	pageStr, err := context.GetForm("page")
	if err != nil {
		log.Println("表单获取失败：", err)
		categoryTemplate.WriteError(context.W, errors.New("系统错误，请联系管理员!!"))
		return
	}
	if pageStr == "" {
		pageStr = "1"
	}
	page, _ := strconv.Atoi(pageStr)
	//每页显示的数量
	pageSize := 5
	categoryResponse, err := service.GetPostsByCategoryId(cId, page, pageSize)
	if err != nil {
		categoryTemplate.WriteError(context.W, err)
		return
	}
	categoryTemplate.WriteData(context.W, categoryResponse)
}
